<?php
/**
 * 权限管理
 * @author  matt@178.com
 *
 */
class admin_controller_admin_priv extends AdminController {
	/**
	 * 
	 *
	 * @var admin_model_resource
	 */
	var $r;
	/**
	 * 
	 *
	 * @var admin_model_role
	 */
	var $role;
	/**
	 * 
	 *
	 * @var admin_model_user
	 */
	var $user;
	
	public function __construct($app, $controller, $standalone=true){
		parent::__construct($app, $controller, $standalone);
		$this->r = Model::factory('admin_model_resource');
		$this->role = Model::factory('admin_model_role');
		$this->user = Model::factory('admin_model_user');
	}
	
	/**
	 * @name 导入权限表单
	 *
	 */
	public function import_form_action(){
		$this->ret = self::success($this->r->selectList());
	}
	
	/**
	 * @name 导入权限
	 *
	 */
	public function import_action(){
		$apps = glob(APP_PATH."/*", GLOB_ONLYDIR );
		foreach($apps as $app){
			$path = $app."/controller/admin/";
			if(file_exists($path)){
				admin_util_priv::import($path);
			}
		}
		$this->_renderJSJump();
		$this->ret = self::success(array('message'=>'导入成功'));
	}
	
	/**
	 * @name 删除操作资源
	 *
	 */
	public function delete_resource_action(){
		$this->_renderJSJump();
		$flag = $this->r->remove($this->id);
		$this->ret = self::success(array('message'=>($flag?'删除成功':'删除失败')));
	}
	/**
	 * @name 设置角色权限表单
	 *
	 */
	public function set_role_form_action(){
		$role = $this->role->select($this->rid);
		$rolePriv = $this->role->selectRolePriv($this->rid);
		$priv = array();
		foreach($rolePriv as $p){
			array_push($priv, $p['resource']);
		}
		$this->ret = self::success(array('role'=>$role, 'priv'=>$priv, 'resources'=>$this->r->selectList()));
	}
	
	/**
	 * @name 设置角色权限
	 *
	 */
	public function set_role_action(){
		$resource_ids = _('resource_id', array(), URL_PARAMETER_ARRAY);
		$this->role->cleanPriv($this->rid);
		foreach($resource_ids as $res_id){
			$this->role->savePriv($this->rid, $res_id);
		}
		$this->_renderJSJump();
		$this->ret = self::success(array('message'=>'设置保存成功'));
	}
	
	/**
	 * @name 设置用户权限表单
	 *
	 */
	public function set_user_form_action(){
		$user = $this->user->select($this->uid);
		$roles = $this->user->selectUserRole($this->uid);
		$role = array();
		foreach($roles as $row){
			array_push($role, $row['role']);
		}
		$privs = $this->user->selectUserPriv($this->uid);
		$priv = array();
		foreach($privs as $row){
			array_push($priv, $row['resource']);
		}
		$this->ret = self::success(array(
			'user'=>$user,
			'role'=>$role,
			'priv'=>$priv,
			'resources'=>$this->r->selectList(),
			'roles'=>$this->role->selectList()
		));
	}
	
	/**
	 * @name 设置用户权限
	 *
	 */
	public function set_user_action(){
		$resource_ids = _('resource_id', array(), URL_PARAMETER_ARRAY);
		$this->user->cleanPriv($this->uid);
		foreach($resource_ids as $res_id){
			$this->user->savePriv($this->uid, $res_id);
		}
		$role_ids = _('role_id', array(), URL_PARAMETER_ARRAY);
		$this->user->cleanRole($this->uid);
		foreach($role_ids as $role_id){
			$this->user->saveRoles($this->uid, $role_id);
		}
		$this->_renderJSJump();
		$this->ret = self::success(array('message'=>'设置保存成功'));
	}
}